package com.graduate.daydayup.fragment.index

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.TimeUtils
import com.graduate.daydayup.App
import com.graduate.daydayup.R
import com.graduate.daydayup.activity.*
import com.graduate.daydayup.adpter.LessonAdapter
import com.graduate.daydayup.base.BaseFragment
import com.graduate.daydayup.bean.HotBean

import com.graduate.daydayup.room.MyRoomDataBase
import com.graduate.daydayup.room.Schedule
import kotlinx.android.synthetic.main.fragment_index.*

import java.text.SimpleDateFormat

class IndexFragment : BaseFragment() {
    override fun getLayout()= R.layout.fragment_index


    override fun initViewIds(view: View?) {
    }
    lateinit var lessonAdapter:LessonAdapter
    override fun onResume() {
        super.onResume()
        Log.d("onResume","onResume")
        refreshLayout.autoRefresh()
        refreshLayout.setOnRefreshListener {
            refreshData()
        }
        refreshLayout.finishRefresh(2000,true);
    }


    fun refreshData(){
        var day=0
        val chineseWeek = TimeUtils.getChineseWeek(TimeUtils.getNowDate())
        when(chineseWeek){
            "周一"->day=1
            "周二"->day=2
            "周三"->day=3
            "周四"->day=4
            "周五"->day=5
            "周六"->day=6
            "周日"->day=7
        }
        var all = MyRoomDataBase.getInstance(App.instance!!).scheduleDao().getAllByDay(day)

        if (all!=null&&all.size>0){
            rl_without.visibility=View.GONE
            refreshLayout.visibility=View.VISIBLE
            stv_todaylesson.setCenterString(all.size.toString())
            lessonAdapter = LessonAdapter(R.layout.layout_today, all)
            rv_todaylesson.adapter= lessonAdapter
            rv_todaylesson.layoutManager=LinearLayoutManager(activity)
            lessonAdapter.setOnItemClickListener(object :LessonAdapter.OnItemClickListener{
                override fun onItemClick(pos: Int) {
                    val intent = Intent(activity, EditScheduleActivity::class.java)
                    intent.putExtra("id",all.get(pos).id)
                    intent.putExtra("day",all[pos].day+1)
                    intent.putExtra("start",all[pos].start)
                    intent.putExtra("step",all[pos].step)
                    startActivity(intent)
                }
            })
        }else{
            stv_todaylesson.setCenterString("0")
            refreshLayout.visibility=View.GONE
            rl_without.visibility=View.VISIBLE
            rl_without.setOnClickListener {
                val intent = Intent(activity, LessonAddActivity::class.java)
                intent.putExtra("day",day)
                startActivity(intent)
            }
        }

    }
    override fun initView(view: View?) {
        stv_weiboresou.setOnSuperTextViewClickListener {
            ActivityUtils.startActivity(HotNewsActivity::class.java)
        }
    }

    @SuppressLint("SimpleDateFormat")
    override fun initData(savedInstanceState: Bundle?) {
        val currentDate= SimpleDateFormat("yyyy-MM-dd")
        tv_date.text=TimeUtils.getNowString(currentDate)
        tv_dayofmonth.text=TimeUtils.getChineseWeek(TimeUtils.getNowDate())
    }


}